<?php

/**
 * ApplicationBehavior class
 *
 * @author Julot <andy.julot@gmail.com>
 * @since Monday, January 30, 2012. 02:39 PM
 */
class ApplicationBehavior extends CBehavior {

	public function events() {
		$data = array('onBeginRequest'=>'beginRequest');
		return array_merge(parent::events(), $data);
	}

	public function beginRequest() {
		$owner = $this->owner;
//		if (isset($_POST['_language'])) {
//			$owner->user->setState('language', $_POST['_language']);
//		}
//		if ($owner->user->getState('language')) {
//			$owner->language = $owner->user->getState('language');
//		}
//		else $owner->language = 'en';

		if (isset($_POST['_theme'])) {
			$owner->user->setState('theme', $_POST['_theme']);
			if (!$owner->user->isGuest) {
				$user = User::model()->findByPk($owner->user->id);
				$user->profile->theme = $_POST['_theme'];
				$user->profile->save();
			}
		}
		if ($owner->user->hasState('theme')) {
			$owner->theme = $owner->user->getState('theme');
		} else {
			$owner->theme = 'leschaco';
		}
		$theme = $owner->theme->name;

		$module = Yii::app()->getModule('rights');
		$module->appLayout = "webroot.themes.$theme.views.layouts.main";
	}

}
